package com.blogs.service;

import com.blogs.entity.BlogPublicEntry;
import com.blogs.entity.BlogDraftFeed;
import com.blogs.entity.SendBlogEntry;
import com.blogs.tools.TbBlogDraft;
import com.cnblogs.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.os.IBinder;

public class SendBlogsService extends Service {
	private static volatile int serviceCount = 0;
	private NotificationManager nm;
	private Notification baseNF;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		if (intent != null) {
			serviceCount++;
			final int draft_id = intent.getIntExtra("draft_id", 0);
			BlogPublicEntry bpe = new BlogPublicEntry();
			bpe.sid = intent.getStringExtra("sid");
			bpe.txbTitle = intent.getStringExtra("txbTitle");
			bpe.EditorBody = intent.getStringExtra("EditorBody");
			bpe.name_site_categroy = intent.getStringExtra("name_site_categroy");
			bpe.name_site_categroy_id = intent.getIntExtra("name_site_categroy_id", -1);
			bpe.cbHomeCandidate = intent.getBooleanExtra("cbHomeCandidate", false);
			bpe.cbIsPublishToSiteHome = intent.getBooleanExtra("cbIsPublishToSiteHome", false);
			bpe.chkDisplayHomePage = intent.getBooleanExtra("chkDisplayHomePage", false);
			bpe.txbExcerpt = intent.getStringExtra("txbExcerpt");
			bpe.txbTag = intent.getStringExtra("txbTag");

			final BlogPublicEntry fbpe = bpe;
			setNotification(R.drawable.queue_icon_send, "博文发送", "博文发送中");
			new SendBlogs(bpe, new SendBlogs.IMsgCallBack() {

				public void onSuccess(SendBlogEntry sce) {
					// TODO Auto-generated method stub
					if (sce.IsSuccess) {
						setNotification(R.drawable.queue_icon_success, "博文发送", "博文发表成功");
						if(draft_id!=0)
						{
							TbBlogDraft tb = new TbBlogDraft(SendBlogsService.this);
							tb.Delete(draft_id+"");
							tb.DbClose();
							Intent intent = new Intent("BlogDraftReceiver");
							sendBroadcast(intent);
						}
					} else {
						String err_text = "";
						if (sce.Message.equals("用户登录")) {
							err_text = "请先登录";
						} else if (!sce.Message.equals("")) {
							setNotification(R.drawable.queue_icon_success, "博文发送", sce.Message);
							err_text = sce.Message;
						} else {
							setNotification(R.drawable.queue_icon_success, "博文发送", "未知错误");
							err_text = "未知错误";
						}
						onError(draft_id,err_text,fbpe);
					}
					serviceCount--;
					if (serviceCount == 0)
						stopTheServer();
				}

				public void onFailed() {
					// TODO Auto-generated method stub
					setNotification(R.drawable.queue_icon_success, "博文发送", "网络错误");
					onError(draft_id,"网络错误",fbpe);
					serviceCount--;
					if (serviceCount == 0)
						stopTheServer();
				}
			}).Send();
		}
		return super.onStartCommand(intent, flags, startId);
	}
	private void onError(int draft_id,String err_text,BlogPublicEntry fbpe)
	{
		TbBlogDraft tb = new TbBlogDraft(SendBlogsService.this);
		if (draft_id == 0) {
			BlogDraftFeed feed = new BlogDraftFeed();
			feed.txbTitle = fbpe.txbTitle;
			feed.EditorBody = fbpe.EditorBody;
			feed.name_site_categroy_id = fbpe.name_site_categroy_id;
			feed.cbHomeCandidate = fbpe.cbHomeCandidate;
			feed.cbIsPublishToSiteHome = fbpe.cbIsPublishToSiteHome;
			feed.chkDisplayHomePage = fbpe.chkDisplayHomePage;
			feed.txbExcerpt = fbpe.txbExcerpt;
			feed.txbTag = fbpe.txbTag;
			feed.err_text = err_text;
			tb.Insert(feed);
		}
		else
		{
			ContentValues values=new ContentValues();
			values.put("txbTitle", fbpe.txbTitle);
			values.put("EditorBody", fbpe.EditorBody);
			values.put("name_site_categroy_id", fbpe.name_site_categroy_id);
			values.put("cbHomeCandidate", fbpe.cbHomeCandidate);
			values.put("cbIsPublishToSiteHome", fbpe.cbIsPublishToSiteHome);
			values.put("chkDisplayHomePage", fbpe.chkDisplayHomePage);
			values.put("txbExcerpt", fbpe.txbExcerpt);
			values.put("txbTag", fbpe.txbTag);
			values.put("err_text", err_text);
			tb.Update(draft_id+"", values);
		}
		tb.DbClose();
		Intent intent = new Intent("BlogDraftReceiver");
		sendBroadcast(intent);// 发送评论失败广播
	}
	private void setNotification(int icon, String title, String con) {
		baseNF = new Notification(icon, con, System.currentTimeMillis());
		baseNF.flags = Notification.FLAG_AUTO_CANCEL;
		Intent nintent = new Intent();
		nintent.setAction(Intent.ACTION_MAIN);
		nintent.addCategory(Intent.CATEGORY_LAUNCHER);
		nintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		nintent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent pendIntent = PendingIntent.getActivity(SendBlogsService.this, 0, nintent, 0);
		baseNF.setLatestEventInfo(SendBlogsService.this, title, con, pendIntent);
		nm.notify(R.string.app_name, baseNF);
		nm.cancelAll();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private void stopTheServer() {
		this.stopSelf();
	}
}
